#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   snmp_disk.sh
# Version:    1.0
# Date:       2013/11/14
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-12SNMP-03NET-SNMP测试-07磁盘检查
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/14
#             - Start snmp service, check disk.
# ----------------------------------------------------------------------

##! @TODO: setup, start snmp service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_setup()
{
    serv=snmpd

    # check command exists
    exists snmpwalk
    EchoResult "snmpwalk command"

    # back snmpd.conf
    CONF=/etc/snmp/snmpd.conf

    [ -f ${CONF} ] && cp ${CONF} ${CONF}.bak
    EchoResult "backup ${CONF}"

    server_ip=`ifconfig -a | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
    [ -n ${server_ip} ]
    EchoResult "get local server ip"

    # update snmpd.conf
    cat > ${CONF} <<-EOF
# sec.name source community
com2sec local localhost public
com2sec mynetwork ${server_ip} public
# groupName securityModel securityName
group MyRWGroup v1 local
group MyRWGroup v2c local
group MyRWGroup usm local
group MyROGroup v1 mynetwork
group MyROGroup v2c mynetwork
group MyROGroup usm mynetwork
# name incl/excl subtree mask(optional)
view all included .1 80
# group context sec.model sec.level prefix read write notif
access MyROGroup "" any noauth exact all none none
access MyRWGroup "" any noauth exact all all none
# Make sure mountd is running
proc mountd

# Make sure there are no more than 4 ntalkds running, but 0 is ok too.
proc ntalkd 4

# Make sure at least one sendmail, but less than or equal to 10 are running.
proc sendmail 10 1

# a simple hello world
exec echotest /bin/echo hello world

# Check the / partition and make sure it contains at least 10 megs.
disk / 10000

# Check for loads:
load 12 14 14
EOF

    # restart
    restart_daemon ${serv}
    EchoResult "service ${serv} restart"

    # check snmpd restart
    status_daemon ${serv}
    netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} status: restart"


    trap do_cleanup INT EXIT

}

##! @TODO: snmpwalk
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    echo "snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.9"
    TMP_LOG=`mktemp`
    snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.9 > ${TMP_LOG}
    # check
    grep "UCD-SNMP-MIB::dsk" ${TMP_LOG}
    EchoResult "snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.9, check ${TMP_LOG}"

}

##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_cleanup()
{
    mv -f ${CONF}.bak ${CONF}
    stop_daemon ${serv}
}

# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh

do_setup
do_test
EchoResult "testcase: ${serv} localhost system disk, you should check information yourself@@@"
